package com.example.gzysmartcity.ui.fragment.home;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentSearchBinding;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.ui.adapter.SearchListRvAdapter;
import com.example.gzysmartcity.ui.adapter.SearchNewsAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.SearchViewModel;

import org.jetbrains.annotations.NotNull;

public class SearchFragment extends BaseFragment<FragmentSearchBinding> {
    @Override
    protected FragmentSearchBinding viewBinding() {
        return FragmentSearchBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SearchViewModel searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
        b.tvCancelSeach.setOnClickListener(v -> remove());
        b.rvSearch.setLayoutManager(new GridLayoutManager(requireContext(), 4));
        b.rvSeachTuijian.setLayoutManager(new LinearLayoutManager(getContext()));
        b.searchSearch.setSubmitButtonEnabled(true);
        searchViewModel.getAllNews();
        searchViewModel.getMutableLiveData().observe(getViewLifecycleOwner(), state -> {
            if (state == State.SUCCESS) {
                SearchListRvAdapter searchListRvAdapter = new SearchListRvAdapter();
                searchListRvAdapter.setNewInstance(searchViewModel.getSearchLists());
                SearchNewsAdapter searchNewsAdapter = new SearchNewsAdapter(R.layout.rv_item_serach_news);
                searchNewsAdapter.setNewInstance(searchViewModel.getNewsList());
                b.rvSeachTuijian.setAdapter(searchNewsAdapter);
                b.rvSearch.setAdapter(searchListRvAdapter);
                searchNewsAdapter.setOnItemClickListener((adapter, view1, position) -> {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("html", searchViewModel.getContent(position));
                    bundle.putBoolean("isSearch",true);
                    jump(NewsXqFragment.class, bundle, "news");
                });
                //
                b.imageNoSearch.setOnClickListener(v -> {
                    searchViewModel.clear();
                    searchListRvAdapter.notifyDataSetChanged();
                    ToastUtils.showShort("清除成功");
                });
                //
                b.searchSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        b.searchSearch.setIconified(true);
                        searchViewModel.search(query);
                        searchListRvAdapter.notifyItemInserted(searchViewModel.getSearchListSize());
                        searchNewsAdapter.notifyDataSetChanged();
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
            } else {
                ToastUtils.showShort("加载失败");
            }
        });
    }

    @Override
    protected String setTitle() {
        return null;
    }

    @Override
    protected boolean showBack() {
        return false;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
